package cn.wyj.org.controller;

import cn.wyj.basic.anno.LogAnno;
import cn.wyj.basic.anno.PreAuthorize;
import cn.wyj.basic.util.AjaxResult;
import cn.wyj.basic.util.PageList;
import cn.wyj.org.domain.Department;
import cn.wyj.org.query.DepartmentQuery;
import cn.wyj.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API", description = "部门相关的CRUD功能")
@LogAnno
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 添加/修改
     */
    @PutMapping
//    @LogAnno(method = "addOrUpdate", methodDes = "添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Department deparment){
        if (null == deparment.getId()){
            // 添加
            departmentService.add(deparment);
        }else {
            // 修改
            departmentService.update(deparment);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除 delete localhost:8080/dept/1
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有部门数据")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询", sn = "department:findById")
    public Department findById(@ApiParam(value = "ID属性", required = true) @PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
    @LogAnno(method = "queryByPage", methodDes = "分页查询部门")
    @PreAuthorize(name = "分页查询", sn = "department:queryByPage")
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery query){
        return departmentService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        departmentService.batchDel(ids);
        return AjaxResult.me().success();
    }
    @PostMapping("/tree")
    @PreAuthorize(name = "部门树", sn = "department:deptTree")
    public List<Department> deptTree(){

        return departmentService.deptTree();
    }
}
